package PowerWord.UI.Exercise;

import java.util.ArrayList;
import java.util.List;

import PowerWord.Core.Plan.SingleExercise;
import PowerWord.UI.R;
import android.util.Pair;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class SingleChoiceHelper extends ExerciseUIHelper {
	
	boolean mode = true;
	
	List<Pair<TextView, RadioGroup>> subjects = new ArrayList<Pair<TextView, RadioGroup>>();
	
	protected void Init(ExerciseActivity context, int m, boolean random){
		super.Init(context, m, random);
		mode = m == 0;
		paper = new SingleExercise(2);
		if(random){
			paper.Random();
		}
		subjects.add(Pair.create((TextView)father.findViewById(R.id.Question1), (RadioGroup)father.findViewById(R.id.Options1)));
		subjects.add(Pair.create((TextView)father.findViewById(R.id.Question2), (RadioGroup)father.findViewById(R.id.Options2)));
		ChangeMode(mode);
	}
	
	protected void UpdateContent(){
		paper.Prepare();
		SingleExercise sinPaper = (SingleExercise)paper;
		int size = paper.GetCurGroupSize();
		int i = 0;
		for(Pair<TextView, RadioGroup> subject : subjects){
			if(i < size){
				subject.first.setVisibility(View.VISIBLE);
				subject.second.setVisibility(View.VISIBLE);
				subject.first.setText(sinPaper.GetQuestion(mode, i));
				subject.second.check(-1);
				for(int j=0; j<SingleExercise.OptionSize; ++j){
					RadioButton option = (RadioButton)subject.second.getChildAt(j);
					option.setText(sinPaper.GetOption(mode, i, j));
					option.setPressed(false);
				}
			}
			else{
				subject.first.setVisibility(View.INVISIBLE);
				subject.second.setVisibility(View.INVISIBLE);
			}
			++i;
		}
	}
	
	protected void SaveScore(){
		int i = 0;
		for(Pair<TextView, RadioGroup> subject : subjects){
			int usrAnswer = -1;
			RadioGroup options = subject.second;
			int childId = options.getCheckedRadioButtonId();
			for(int j=0; j<SingleExercise.OptionSize; ++j){
				if(childId == options.getChildAt(j).getId()){
					usrAnswer = j;
					break;
				}
			}
			((SingleExercise)paper).SaveScore(i, usrAnswer);
			++i;
		}
	}
	
	protected void ShowRightAnswer(){
		int i = 0;
		for(Pair<TextView, RadioGroup> subject : subjects){
			subject.second.getChildAt(((SingleExercise)paper).GetAnswer(i)).setPressed(true);
			++i;
		}
	}
	
	private void ChangeMode(boolean b){
		mode = b;
		double qusWeight = mode ? 0.8 : 0.5;
		double optWeight = mode ? 0.1 : 0.5;
		float qusTextType = mode ? 20 : 15;
		float optTextSize = mode ? 15 : 20;
		int i = 0;
		for(Pair<TextView, RadioGroup> subject : subjects){
			TextView question = subject.first;
			LayoutParams layParams = (LayoutParams) question.getLayoutParams();
			layParams.weight = (float) qusWeight;
			question.setLayoutParams(layParams);
			question.setTextSize(qusTextType);
			
			RadioGroup options = subject.second;
			layParams = (LayoutParams) options.getLayoutParams();
			layParams.weight = (float) optWeight;
			options.setLayoutParams(layParams);
			options.check(-1);
			for(int j=0; j<SingleExercise.OptionSize; ++j){
				RadioButton option = (RadioButton)subject.second.getChildAt(j);
				option.setTextSize(optTextSize);
			}
			++i;
		}
	}
}
